<template>
	<view class="index">
		<view class="content">
			<view class="input-list">
				<!-- input输入框 -->
				<input class="input-item" v-if="focus" adjust-position="false" auto-blur="true" @blur="inputCodeBlur"
					@input="inputCode" :focus="focus" v-model="code" @focus="inputFocus" type="number" oneTimeCode
					maxlength="6" />
				<!-- 验证码输入框 -->
				<view class="code-list" @click="focusClick">
					<view class="code-item" v-for="(item,index) in 5" :key="index"
						:style="(index == code.length && focus ? 'border-color:#3c9cff;':'')">{{code[index]}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				code: '', // 验证码字段
				focus: true, // input是否聚焦
				item: [],
			}
		},
		onLoad(option) {
			this.item = JSON.parse(option.item)
		},

		methods: {
			// 获取验证码
			getCode() {
				this.codeBtn.isCode = false
				this.codeBtn.codeNumber = 2
				this.timer = setInterval(() => {
					if (this.codeBtn.codeNumber == 0) {
						clearInterval(this.timer)
						return
					}
					this.codeBtn.codeNumber--
				}, 1000)
			},
			// 点击聚焦输入框
			focusClick() {
				this.focus = true
			},
			// 输入框失去焦点
			inputCodeBlur(e) {
				let value = e.detail.value
				this.focus = false
			},
			// 输入框聚焦时触发（没用到）
			inputFocus(e, height) {
				console.log(e)
			},
			// 输入框内容变化事件
			inputCode(e) {
				let value = e.detail.value
				this.code = value
				if (this.code.length == 5) {
					console.log(this.code)
					//判断验证码是否正确
					if (this.item.passwordCode == this.code) {
						uni.navigateTo({
							url: `/pages/afterSaleVue/evaluate/evaluate?item=${JSON.stringify(this.item)}`
						});
					}else{
						uni.showToast({
							title: '口令码错误，请重试！',
							icon:'none',
							duration: 2000
						});
						this.code = '';
					}
				}
			},
		}
	}
</script>
<style lang="scss" scoped>
	.index {
		padding: 30rpx;

		.content {
			padding: 20rpx;

			.phone-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 30rpx;

				.phone-input {
					width: calc(100% - 240rpx);
					padding: 16rpx;
					border-bottom: 1rpx solid #eee;
				}

				.get-code {
					text-align: center;
					width: 170rpx;
					font-size: 26rpx;
					border-radius: 50rpx;
					padding: 10rpx 0rpx;
					background: #3c9cff;
					color: #fff;
				}
			}

			.input-list {
				display: flex;
				align-items: center;

				.input-item {

					width: 0rpx;
				}

				.code-list {
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: space-between;

					.code-item {
						width: 80rpx;
						height: 80rpx;
						text-align: center;
						line-height: 80rpx;
						border: 1rpx solid #000000;
						border-radius: 10rpx;
					}
				}
			}
		}
	}
</style>